/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.github.fafaldo.fabtoolbar.sample.adapter;

import ohos.agp.colors.RgbColor;
import ohos.agp.components.BaseItemProvider;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.ComponentState;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.Text;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.components.element.StateElement;
import ohos.app.Context;

import com.github.fafaldo.fabtoolbar.sample.ResourceTable;


/**
 * @since 2021-07-06
 */
public class ContentProvider extends BaseItemProvider {
    private Context mContext;
    private int mItemSize;

    public ContentProvider(Context context, int size) {
        mContext = context;
        mItemSize = size;
    }

    @Override
    public int getCount() {
        return mItemSize;
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public Component getComponent(int position, Component component, ComponentContainer componentContainer) {
        ContentHolder contentHolder;
        if (component == null) {
            component = LayoutScatter.getInstance(mContext).parse(ResourceTable.Layout_item, null, false);
            contentHolder = new ContentHolder(component);
            component.setTag(contentHolder);
        } else {
            contentHolder = (ContentHolder) component.getTag();
        }
        contentHolder.contentText.setText("Test " + position);

        setBackground(component);

        return component;
    }

    class ContentHolder {
        Text contentText;

        ContentHolder(Component component) {
            this.contentText = (Text) component.findComponentById(ResourceTable.Id_tv_item_test_content);
        }
    }

    private void setBackground(Component component) {
        StateElement stateElement = new StateElement();

        ShapeElement pressedShapeElement = new ShapeElement();
        pressedShapeElement.setShape(ShapeElement.RECTANGLE);
        pressedShapeElement.setRgbColor(RgbColor.fromArgbInt(0xFFBEBEBE));
        stateElement.addState(new int[]{ComponentState.COMPONENT_STATE_PRESSED}, pressedShapeElement);

        ShapeElement normalShapeElement = new ShapeElement();
        normalShapeElement.setRgbColor(RgbColor.fromArgbInt(0xFFFFFFFF));
        stateElement.addState(new int[]{ComponentState.COMPONENT_STATE_EMPTY}, normalShapeElement);

        component.setBackground(stateElement);
    }
}
